package com.nbk.composition.dao.convertor

import com.nbk.composition.dao.bean.Action
import com.nbk.composition.utils.fromJson
import com.nbk.composition.utils.toJson
import org.apache.ibatis.type.BaseTypeHandler
import org.apache.ibatis.type.JdbcType
import org.apache.ibatis.type.MappedJdbcTypes
import java.sql.CallableStatement
import java.sql.PreparedStatement
import java.sql.ResultSet

//@MappedTypes(value = [Geometry::class])
@MappedJdbcTypes(JdbcType.LONGVARCHAR)
class ActionTypeHandler : BaseTypeHandler<Action>() {

    override fun getNullableResult(resultSet: ResultSet, s: String): Action? {
        return resultSet.getString(s)?.fromJson()
    }

    override fun getNullableResult(resultSet: ResultSet, i: Int): Action? {
        return resultSet.getString(i)?.fromJson()
    }

    override fun getNullableResult(callableStatement: CallableStatement, i: Int): Action? {
        return callableStatement.getString(i)?.fromJson()
    }

    override fun setNonNullParameter(p0: PreparedStatement?, p1: Int, p2: Action?, p3: JdbcType?) {
        p0?.setString(p1, p2?.toJson() ?: "")
    }
}